const router = require('koa-router')()
const {
  getList,
  getDetail,
  newAppointment,
  countAppointment,
  totalAppointment
} = require('../controller/appointment')

const { SuccessModel, ErrorModel } = require('../model/resModel')

router.prefix('/api/appointment')

router.get('/mylist', async (ctx, next) => {
  const userId = ctx.query.userId
  const data = await getList(userId)
  ctx.body = new SuccessModel(data)  
})

router.get('/count', async (ctx, next) => {
  const body = ctx.request.body
  const data = await countAppointment(body)
  ctx.body = new SuccessModel(data)  
})
 
router.post('/add',  async function (ctx, next) {
  const body = ctx.request.body
  const check1 = await countAppointment(body)  
  if(check1 > 0){ 
	ctx.body = new SuccessModel("已预约成功") 
	return
  }
  
  const check2 = await totalAppointment(body)  
  if(check2 > 1){
  	ctx.body = new SuccessModel("预约次数不能超过2次") 
  	return
  }
  const data = await newAppointment(body)
  if (data){
	ctx.body = new SuccessModel(data,"预约成功") 
  }else{
	ctx.body = new ErrorModel(data,'预约失败') 
  }
})
 

module.exports = router
